package com.huixuebao.api.util;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SendSms {

    public static String sendCaptchaCode(String mobile, String captchaCode){
        return send(mobile,"SMS_205444599","{'code':'"+captchaCode+"'}");
    }

    public static String sendMessage(String mobile, String teacherName, String templateCode){
        return send(mobile,"SMS_189836590","{'name':'"+ teacherName +"','teacherCode':'"+ templateCode +"'}");
    }

    public static String send(String mobile,String templateCode,String templateParam) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G1A22AVTq4iuQqyQR7j", "8ldH31BXb4lqrpkqpNEsN481eE3S3J");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", "汇学保");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", templateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JSONObject jsonObject =  JSONUtil.parseObj(response.getData());
            if("OK".equals(jsonObject.get("Message")) && "OK".equals(jsonObject.get("Code"))){
                return "OK";
            }else{
                return (String)jsonObject.get("Message");
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "短信发送失败";
    }

    public static void main(String[] args){
        SendSms.sendCaptchaCode("17701618951","654321");
        //SendSms.sendMessage("17701618951","name","123456");
    }
}
